﻿using HandyControl.Controls;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace UI.View.Base.CarView
{
    /// <summary>
    /// List.xaml 的交互逻辑
    /// </summary>
    public partial class List : Page
    {
        RestClient client = new RestClient(new Uri(Comm.TempData.BaseUrl));
        ViewModel.CarViewModel vm;
        public List()
        {
            InitializeComponent();

            vm = DataContext as ViewModel.CarViewModel;
            vm.LoadData();
        }



        Model.DataContext context = new Model.DataContext();
        private async void btn_save_click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(vm.CurrCar.CarNo))
            {
                Growl.WarningGlobal("车牌号码不能为空！");
                return;
            }

            if (string.IsNullOrEmpty(vm.CurrCar.CarBrand))
            {
                Growl.WarningGlobal("汽车品牌不能为空！");
                return;
            }

            if (string.IsNullOrEmpty(vm.CurrCar.CarType))
            {
                Growl.WarningGlobal("汽车型号不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(vm.CurrCar.CarColor))
            {
                Growl.WarningGlobal("汽车型号不能为空！");
                return;
            }
            if (vm.IsEditCar)
            {
                if (vm.CurrCar != null)
                {
                    try
                    {
                        context.DB.Updateable<Model.OwnerCar>(vm.CurrCar).ExecuteCommand();
                        Growl.SuccessGlobal("修改成功！");
                    }
                    catch (Exception ex)
                    {
                        Growl.FatalGlobal(ex.Message);

                    }
                   

                     
                }
            }
            else
            {
                if (vm.CurrCar!= null)
                {
 

                    try
                    {

                        context.DB.Insertable<Model.OwnerCar>(vm.CurrCar).ExecuteCommand();
                        Growl.SuccessGlobal("修改成功！");
                    }
                    catch (Exception ex)
                    {
                        Growl.FatalGlobal(ex.Message);

                    }

                }
            }


           vm.LoadData();
        }

        private void btn_Cancle_Click(object sender, RoutedEventArgs e)
        {
            sp_edit.IsEnabled = false;
        }

        private void btn_Add_Click(object sender, RoutedEventArgs e)
        {
            sp_edit.IsEnabled = true;
            vm.CurrCar = new Model.OwnerCar();
            vm.IsEditCar = false;
        }

        private void btn_Edit_Click(object sender, RoutedEventArgs e)
        {
            sp_edit.IsEnabled = true;
            
            vm.IsEditCar = true;
        }

        

        private async void btn_Delete_Click(object sender, RoutedEventArgs e)
        {
            var res = new RestRequest("/car/by-id/" + vm.CurrCar.Id, Method.DELETE);


            var ret = await client.DeleteAsync<SingleDataResponse<int>>(res);
            if (ret.succeeded)
            {
                HandyControl.Controls.Growl.SuccessGlobal("删除成功！");
            }
            else
            {
                HandyControl.Controls.Growl.ErrorGlobal("删除失败！");
            }

            vm.LoadData();
        }
    }
}
